﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; // pour DllImport
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace ScreenShoter
{
    public class gestionCapture
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetDesktopWindow();

        public enum CaptureMode
        {
            Screen, Window
        }

        public Bitmap Capture(CaptureMode mode = CaptureMode.Window)
        {
            return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
        }

        //public static Bitmap Capture(Control c)
        //{
        //    return Capture(c.Handle);
        //}

        public Bitmap Capture(IntPtr handle)
        {
            Rectangle bounds;
            var rect = new Rect();
            GetWindowRect(handle, ref rect);
            bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
            //CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);

            var result = new Bitmap(bounds.Width, bounds.Height);
            using (var g = Graphics.FromImage(result))
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);

            return result;
        }

        public void ImageSave(Bitmap Img, string formatSelectionné, string PathChoisi)
        {
            if (Directory.Exists(PathChoisi))
            {
                string format;
                ImageFormat imgFormat;

                if (formatSelectionné == "PNG")
                {
                    format = "png";
                    imgFormat = ImageFormat.Png;
                }
                else
                {
                    if (formatSelectionné == "JPEG")
                    {
                        format = "jpg";
                        imgFormat = ImageFormat.Jpeg;
                    }
                    else
                    {
                        format = "gif";
                        imgFormat = ImageFormat.Gif;
                    }
                }

                string Name = PathChoisi + @"\" + "Screen_" + (DateTime.Now.ToFileTime()) + "." + format;
                Img.Save(Name, imgFormat);
            }
            else
            {
                MessageBox.Show("Le dossier de destination est incorrect.\nMerci d'en choisir un valide.",
                                "Erreur dossier destination",
                                 MessageBoxButtons.OK,
                                 MessageBoxIcon.Error);
            }
        }
    }
}
